import flask
from flask import Flask,jsonify
from flask_cors import *
from modules import gossip_robot,medical_robot,classifier
from utils.json_utils import *
app = Flask(__name__)
CORS(app, supports_credentials=True)

@app.route('/', methods=['GET', 'POST'])
def hello():
    data= flask.request.get_json()['data']
    user=data["user"]
    text=data["msg"]
    user_intent = classifier(text)
    print(user_intent)
    if user_intent in ["greet", "goodbye", "deny", "isbot","thank"]:
        reply = gossip_robot(user_intent)
    elif user_intent == "accept":
        reply = load_user_dialogue_context(user)
        reply = reply.get("choice_answer")
    else:
        reply = medical_robot(text, user)
        if reply["slot_values"]:
            dump_user_dialogue_context(user, reply)
        reply = reply.get("replay_answer")
    return jsonify(reply)

@app.route('/feedback',methods=['GET','POST'])
def save_feedback():
    data = flask.request.get_json()['data']
    dump_feedback(data)
    return jsonify(200)

if __name__=='__main__':
    app.run(host='0.0.0.0', port=9000, debug=True)